package pe.com.csi.pm.regexiv;

import java.util.List;

import pe.com.csi.pm.regexiv.bean.Foto;

import android.content.Context;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.BaseColumns;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;

class ImageAdapter extends BaseAdapter {
	int mGalleryItemBackground;
	private Context mContext;
	private Cursor mCursor;
	private int tipo;
	List<?> fotos;

	public List<?> getFotos() {
		return fotos;
	}

	public void setFotos(List<?> fotos) {
		this.fotos = fotos;
	}

	public ImageAdapter(Cursor cursor, Context c) {
		mContext = c;
		mCursor = cursor;
		TypedArray a = mContext.obtainStyledAttributes(R.styleable.Gallery1);
		mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
		a.recycle();
	}

	public ImageAdapter() {

	}

	public ImageAdapter(Context c, int i) throws Exception {
		mContext = c;
		this.tipo = i;
		TypedArray a = mContext.obtainStyledAttributes(R.styleable.Gallery1);
		mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
		a.recycle();

	}

	@Override
	public int getCount() {
		if (tipo == 1)
			return this.fotos.size();
		else
			return mCursor.getCount();

	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView i = new ImageView(mContext);
		if (tipo == 1) {
			try {
				if (convertView == null) {
					Foto foto = (Foto) fotos.get(position);
					Bitmap bm = BitmapFactory.decodeByteArray(foto.getFoto(), 0, foto.getFoto().length);
					i.setImageBitmap(bm);
					i.setScaleType(ImageView.ScaleType.FIT_XY);
					i.setLayoutParams(new Gallery.LayoutParams(136, 136));
					i.setBackgroundResource(mGalleryItemBackground);
				}
			} catch (Exception e) {
				Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
				e.printStackTrace();
			}
		} else {
			try {
				mCursor.requery();
				if (convertView == null) {
					mCursor.moveToPosition(position);
					int thumbId = mCursor.getInt(mCursor.getColumnIndexOrThrow(BaseColumns._ID));
					Uri thumbUri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + thumbId);

					i.setImageURI(thumbUri);
					i.setScaleType(ImageView.ScaleType.FIT_XY);
					i.setLayoutParams(new Gallery.LayoutParams(136, 136));
					i.setBackgroundResource(mGalleryItemBackground);
				}
			} catch (Exception e) {
				Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
				e.printStackTrace();
			}

		}
		return i;
	}
}